<?php

//--------------------------------------------------------------------------------
// Test if a photo is a user's favorites
//--------------------------------------------------------------------------------
function isFavorite($photo, $user) {
  debug("Checking if photo $photo is a favorite of $user");
  $favorite = 0;
  $query = "SELECT 1 FROM favorites where user='$user' and photo='$photo'";
  debugSQL($query);
  $rs = mysql_query( $query );
  if( $rs > 0 )
    if( mysql_fetch_row ($rs) != null )
      $favorite = 1;
  debug("favorite result: $favorite");
  return $favorite;
}

//--------------------------------------------------------------------------------
// Set / Unset a photo as favorite
//--------------------------------------------------------------------------------
function setFavorite($favorite) {
  global $photo;
  global $pseudo;
  global $action;
  if( !isCurrentUserValidated() ) {
    addFault("You account have been rejected");
    return;
  }
  debug("Setting/Unsetting photo $photo as favorite of $pseudo (favorite=$favorite)");
  if( $favorite ) 
    $query = "INSERT INTO favorites (user,photo) VALUES ('$pseudo', '$photo')";
  else
    $query = "DELETE FROM favorites WHERE user='$pseudo' AND photo='$photo'";
  debugSQL($query);
  $rs = mysql_query( $query );
  pageSwitchBack("photo");
}

?>
